home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / THIN C 2.0 / Projects / wordCount / wordCount.c next >
Encoding:
C/C++ Source or Header  |  1991-05-03  |  648 b   |  43 lines  |  [TEXT/THIN]

  1. #include <stdio.h>
  2.  
  3.  
  4. #define MAX_LINE_LENGTH    200
  5.  
  6. #define C_RETURN    '\n'
  7. #define C_TAB        '\t'
  8. #define C_SPACE        ' '
  9.  
  10. main()
  11. {
  12.     char    line[ MAX_LINE_LENGTH ], *charPtr, inWord;
  13.     int        numWords;
  14.  
  15.     printf( "Type a line of text, please:\n" );
  16.  
  17.     charPtr = line;
  18.     numWords = 0;
  19.     inWord = FALSE;
  20.     
  21.     while ( ( *charPtr = getchar() ) != C_RETURN )
  22.     {
  23.         if ( (*charPtr != C_TAB) && (*charPtr != C_SPACE) )
  24.         {
  25.             if ( ! inWord )
  26.             {
  27.                 inWord = TRUE;
  28.                 numWords++;
  29.             }
  30.         }
  31.         else
  32.             inWord = FALSE;
  33.             
  34.         charPtr++;
  35.     }
  36.     
  37.     printf( "You just typed %d word", numWords );
  38.     
  39.     if ( ( numWords > 1 ) || ( numWords == 0 ) )
  40.         printf( "s." );
  41.     else
  42.         printf( "." );
  43. }